﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using douban.app.Views;
using douban.Core.Apis;
using douban.Core.Repositories;
using Microsoft.Practices.ObjectBuilder2;

namespace douban.Core.ViewModels
{
    public class MainVm:VmBase
    {
        private readonly ConfigRepository _configRepository;
        private bool _loading;

        public MainVm(ConfigRepository configRepository)
        {
            _configRepository = configRepository;
            MeCommand = new RelayCommand(TurnMe);
            InitApp();
        }

        public ICommand MeCommand { get; private set; }

        private void InitApp()
        {
            ExploreTopics = new IncrementalLoadingCollection<Topic>(LoadMoreExplore);
            ExploreTopics.OnLoadMoreStarted += ExploreTopics_OnLoadMoreStarted;
            ExploreTopics.OnLoadMoreCompleted += ExploreTopicsOnOnLoadMoreCompleted;
            MyGroupsTopics = new IncrementalLoadingCollection<Topic>(LoadMoreMyGroup);
            TechTopics = new IncrementalLoadingCollection<Topic>(LoadMoreTech);
            LifeTopics = new IncrementalLoadingCollection<Topic>(LoadMoreLife);
            FashionTopics = new IncrementalLoadingCollection<Topic>(LoadMoreFashion);
            EntTopics = new IncrementalLoadingCollection<Topic>(LoadMoreEnt);
            TravelTopics = new IncrementalLoadingCollection<Topic>(LoadMoreTravel);
            CultureTopics = new IncrementalLoadingCollection<Topic>(LoadMoreCulture);
        }

        private async void ExploreTopicsOnOnLoadMoreCompleted(int count)
        {
            await Task.Delay(3000);
            Loading = false;
        }

        private async void ExploreTopics_OnLoadMoreStarted(uint count)
        {
            Loading = true;
        }

        private async Task<Tuple<IEnumerable<Topic>, bool>> LoadMoreExplore(int start, int count)
        {
            return await SearchTopic(Apis.Api.GpSearchExploreTopics,start, count);
        }

        private async Task<Tuple<IEnumerable<Topic>, bool>> LoadMoreMyGroup(int start, int count)
        {
            return await SearchTopic(Apis.Api.GpsearchMyTopics, start, count);
        }

        private async Task<Tuple<IEnumerable<Topic>, bool>> LoadMoreTech(int start, int count)
        {
            return await SearchTopic(Apis.Api.GpSearchTechTopics, start, count);
        }

        private async Task<Tuple<IEnumerable<Topic>, bool>> LoadMoreLife(int start, int count)
        {
            return await SearchTopic(Apis.Api.GpSearchLifeTopics, start, count);
        }

        private async Task<Tuple<IEnumerable<Topic>, bool>> LoadMoreFashion(int start, int count)
        {
            return await SearchTopic(Apis.Api.GpSearchFashionTopics, start, count);
        }

        private async Task<Tuple<IEnumerable<Topic>, bool>> LoadMoreEnt(int start, int count)
        {
            return await SearchTopic(Apis.Api.GpSearchEntTopics, start, count);
        }

        private async Task<Tuple<IEnumerable<Topic>, bool>> LoadMoreTravel(int start, int count)
        {
            return await SearchTopic(Apis.Api.GpSearchTravelTopics, start, count);
        }

        private async Task<Tuple<IEnumerable<Topic>, bool>> LoadMoreCulture(int start, int count)
        {
            return await SearchTopic(Apis.Api.GpSearchCultureTopics, start, count);
        }

        private async Task<Tuple<IEnumerable<Topic>, bool>> SearchTopic(Func<int, int, Task<IEnumerable<Topic>>> searchTopic, int start, int count)
        {
            try
            {
                var topics = await searchTopic(start, count);

                return await Task.Run(() => Tuple.Create(topics, true));
            }
            catch(Exception ex)
            {
                return new Tuple<IEnumerable<Topic>, bool>(new List<Topic>(),false);
            }
        }


        private async void TurnMe()
        {
            await Navigate(typeof (MePage));
        }

        public override void OnNavigatedFrom(NavigationEventArgs args)
        {
        }

        public override async void OnNavigatedTo(NavigationEventArgs args)
        {
            var authenticate = _configRepository.GetAuthenticate();
            authenticate?.SetCommon();
        }

        public bool Loading
        {
            get { return _loading; }
            set
            {
                _loading = value;
                OnPropertyChanged("Loading");
            }
        }


        public IncrementalLoadingCollection<Topic> ExploreTopics  { get; set; }

        public IncrementalLoadingCollection<Topic> MyGroupsTopics { get; set; }

        public IncrementalLoadingCollection<Topic> TechTopics     { get; set; }

        public IncrementalLoadingCollection<Topic> LifeTopics     { get; set; }

        public IncrementalLoadingCollection<Topic> FashionTopics  { get; set; }

        public IncrementalLoadingCollection<Topic> EntTopics      { get; set; }

        public IncrementalLoadingCollection<Topic> TravelTopics   { get; set; }

        public IncrementalLoadingCollection<Topic> CultureTopics  { get; set; }

        #region 程序集特性访问器

        /// <summary>
        /// 程序集标题
        /// </summary>
        public static string AssemblyTitle
        {
            get
            {
                var attributes = CurrentAssembly.GetCustomAttributes(typeof(AssemblyTitleAttribute)).ToList();
                return attributes.Count == 0 ? "" : ((AssemblyTitleAttribute)attributes[0]).Title;
            }
        }

        /// <summary>
        /// 当前程序集
        /// </summary>
        public static Assembly CurrentAssembly => typeof(MainVm).GetTypeInfo().Assembly;

        /// <summary>
        /// 版本号
        /// </summary>
        public static string AssemblyVersion => CurrentAssembly.GetName().Version.ToString();

        /// <summary>
        /// 描述
        /// </summary>
        public static string AssemblyDescription
        {
            get
            {
                var attributes = CurrentAssembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute)).ToList();
                return attributes.Count == 0 ? "" : ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        /// <summary>
        /// 产品
        /// </summary>
        public static string AssemblyProduct
        {
            get
            {
                var attributes = CurrentAssembly.GetCustomAttributes(typeof(AssemblyProductAttribute)).ToList();
                return !attributes.Any() ? "" : ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        /// <summary>
        /// 版权
        /// </summary>
        public static string AssemblyCopyright
        {
            get
            {
                var attributes = CurrentAssembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute)).ToList();
                return attributes.Count == 0 ? "" : ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        /// <summary>
        /// 公司/作者
        /// </summary>
        public static string AssemblyCompany
        {
            get
            {
                var attributes = CurrentAssembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute)).ToList();
                return attributes.Count == 0 ? "" : ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }

       

        #endregion
    }
}
